package com.zlt.appparking.service.impl;


import com.zlt.appparking.dto.FeedBackDTO;
import com.zlt.appparking.entity.*;
import com.zlt.appparking.mapper.FeedbackMapper;
import com.zlt.appparking.mapper.ParkingOrderMapper;
import com.zlt.appparking.service.AppUserService;
import com.zlt.appparking.service.FeedBackService;
import com.zlt.appparking.utils.StringUtil;
import com.zlt.appparking.vo.ActiveUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.util.List;

@Service
@Slf4j
public class FeedBackServiceImpl implements FeedBackService {

    private FeedBackService feedBackService;

    @Autowired
    @Lazy
    public void setFeedBackService(FeedBackService feedBackService) {
        this.feedBackService = feedBackService;
    }

    @Autowired
    private RedisTemplate redisTemplate;


    @Autowired
    private FeedbackMapper feedbackMapper;

    @Autowired
    private ParkingOrderMapper parkingOrderMapper;


    @Override
    public boolean insertFeedback(FeedBackDTO feedBackDTO) {

        /*
            首先自己是谁

            然后 发过来额feedbackdto中 要有订单编号
            根据定单编号  先找到订单信息  然后订单信息中有  停车场的id   用户的id

         */


        //找到当前活动人
        ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();

        //找到当前的订单表
        ParkingOrder parkingOrder = feedBackService.selectParkingOrderByPoNo(feedBackDTO.getPo_no());
        System.out.println("匹配：" + activeUser.getAuId().equals(parkingOrder.getAuId()));

        if (!(activeUser.getAuId().equals(parkingOrder.getAuId()))) {
            throw new RuntimeException("当前活动人不是订单的所有者");
        }


        //正则  反馈信息 xss    是否合法


        Feedback feedback = new Feedback();
        BeanUtils.copyProperties(feedBackDTO, feedback);


        //设置参数
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String time = StringUtil.getCurrentTime();
        Date date = null;
        try {
            date = simpleDateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        feedback.setFbTime(date);
        feedback.setFbState(false);
        feedback.setAuId(activeUser.getAuId());
        feedback.setPlId(parkingOrder.getPlId());

        int i = feedbackMapper.insertSelective(feedback);

        return i > 0;
    }

    @Override
    public ParkingOrder selectParkingOrderByPoNo(String po_no) {


        ParkingOrderExample parkingOrderExample = new ParkingOrderExample();
        parkingOrderExample.createCriteria().andPoNoEqualTo(po_no);
        List<ParkingOrder> parkingOrders = parkingOrderMapper.selectByExample(parkingOrderExample);
        System.out.println("selectParkingOrder: " + parkingOrders);
        return parkingOrders.isEmpty() ? null : parkingOrders.get(0);
    }
}
